package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.GoodsClassify;
import com.mls.fivedistrict.server.GoodsClassifyService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 */
@RestController("backendGoodsClassifyController")
@RequestMapping("/api/manager")
public class GoodsClassifyController extends BaseController {
    @Autowired
    GoodsClassifyService goodsClassifyService;

    /**
     * 新增商品种类
     *
     * @param goodsClassify
     * @return
     */
    @PostMapping("/v1/goodsClassify")
    public ServiceResponse addGoodsClassify(@RequestBody GoodsClassify goodsClassify) {
        BeanValidator.check(goodsClassify);
        boolean flag = goodsClassifyService.insert(goodsClassify);
        if (flag) {
            return ServiceResponse.createSuccessResponse("添加商品种类成功！");
        } else {
            return ServiceResponse.createErrorResponse("添加商品种类失败！");
        }
    }

    /**
     * 修改商品种类
     * @param goodsClassifyId
     * @param goodsClassify
     * @return
     */
    @PutMapping("/v1/goodsClassify/{goodsClassifyId}")
    public ServiceResponse editGoodsClassify(@PathVariable Long goodsClassifyId, @RequestBody GoodsClassify goodsClassify) {
        Optional.ofNullable(goodsClassifyId).orElseThrow(() -> new CustomerException("要修改的商品id不能为空"));
        Optional.ofNullable(goodsClassify).orElseThrow(() -> new CustomerException("商品分类不能为空"));
        goodsClassify.setId(goodsClassifyId);
        boolean flag = goodsClassifyService.updateById(goodsClassify);
        if (flag) {
            return ServiceResponse.createSuccessResponse("修改商品种类成功！");
        } else {
            return ServiceResponse.createErrorResponse("修改商品种类失败");
        }
    }

    /**
     * 删除商品种类
     * @param goodsClassifyId
     * @return
     */
    @DeleteMapping("/v1/goodsClassify/{goodsClassifyId}")
    public ServiceResponse deleteGoodsClassify(@PathVariable Long goodsClassifyId) {
        Optional.ofNullable(goodsClassifyId).orElseThrow(() -> new CustomerException("要删除的商品种类id不能为空"));
        Optional.ofNullable(goodsClassifyId).orElseThrow(() -> new CustomerException("要删除的商品种类id不能为空"));
        GoodsClassify goodsClassify = new GoodsClassify();
        goodsClassify.setId(goodsClassifyId);
        goodsClassify.setIsDelete(true);
        boolean flag = goodsClassifyService.updateById(goodsClassify);
        if (flag) {
            return ServiceResponse.createSuccessResponse("删除商品种类成功！");
        }else{
            return ServiceResponse.createErrorResponse("删除商品种类失败");
        }
    }

    /**
     * 分页模板
     * @return
     */
    @GetMapping("/v1/goodsClassifies")
    public ServiceResponse getGoodsClassifyList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = goodsClassifyService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<GoodsClassify>(list,5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 根据id查询商品种类
     * @param goodsClassifyId
     * @return
     */
    @GetMapping("/v1/goodsClassify/{goodsClassifyId}")
    public ServiceResponse getGoodsClassifyById(@PathVariable Long goodsClassifyId){
        Optional.ofNullable(goodsClassifyId).orElseThrow(() -> new CustomerException("要查询的商品种类id不能为空"));
        GoodsClassify goodsClassify = goodsClassifyService.selectById(goodsClassifyId);
        if(null!=goodsClassify){
            return ServiceResponse.createSuccessResponse(goodsClassify);
        }else{
            return ServiceResponse.createErrorResponse("商品种类不存在!");
        }
    }
}
